package com.mogician.rabbitmq_demo.activity;

import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.mogician.rabbitmq_demo.Func;
import com.mogician.rabbitmq_demo.R;
import com.mogician.rabbitmq_demo.adapter.MsgAdapter;
import com.mogician.rabbitmq_demo.bean.MyMessage;
import com.mogician.rabbitmq_demo.service.MQPublishService;
import com.mogician.rabbitmq_demo.service.MQSubscribeService;
import com.mogician.rabbitmq_demo.service.TcpClientThread;
import com.mogician.rabbitmq_demo.util.ConnectionUtil;
import com.mogician.rabbitmq_demo.util.DeviceIdHelper;

import java.io.IOException;
import java.util.ArrayList;

public class ChatActivity extends AppCompatActivity
{
    private ObjectMapper mapper;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat);

        mapper = new ObjectMapper();
//        String id = DeviceIdHelper.getUniquePsuedoID();
        String id = System.currentTimeMillis() + "1";

        RecyclerView recyclerView = (RecyclerView)findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        ArrayList<MyMessage> messages = new ArrayList<>();
        MsgAdapter adapter = new MsgAdapter(messages);
        recyclerView.setAdapter(adapter);

        EditText editText = findViewById(R.id.edit_text);
        Button sendBtn = findViewById(R.id.button_send);
        MQPublishService publishService = new MQPublishService(
                ConnectionUtil.MQ_EXCHANGE_NAME, ConnectionUtil.MQ_ROUTING_KEY);
        sendBtn.setOnClickListener(e->
        {
            MyMessage message = new MyMessage(id, editText.getText().toString()
                    , true);
            publishService.publishMessage(message);
            runOnUiThread(()-> adapter.addData(message));
            editText.setText("");
        });

        @SuppressLint("HandlerLeak")
        Handler handler = new Handler()
        {
            @Override
            public void handleMessage(Message msg)
            {
                try {
                    // 获取RabbitMQ的消息数据
                    byte[] messageData = msg.getData().getByteArray("msg");
                    MyMessage message = mapper.readValue(messageData, MyMessage.class);
                    if(message.getId().equals(id))
                        return;
                    message.setSelf(false);
                    Log.e("mq", messageData.toString());
                    runOnUiThread(()-> adapter.addData(message));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        };
        MQSubscribeService subscribeService = new MQSubscribeService(
                handler, ConnectionUtil.MQ_EXCHANGE_NAME, ConnectionUtil.MQ_ROUTING_KEY);

    }
}
